package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.StoreProductDto;
import com.dingreading.cloud.soms.entity.StoreProducts;
import com.dingreading.cloud.soms.entity.table.StoreProductsTableDef;
import com.dingreading.cloud.soms.mapper.StoreProductsMapper;
import com.dingreading.cloud.soms.service.StoreProductsService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-09-11
 */
@Service
public class StoreProductsServiceImpl extends BaseServiceImpl<StoreProductsMapper, StoreProducts, StoreProductsTableDef> implements StoreProductsService {

    @Override
    protected StoreProductsTableDef getTable() {
        return StoreProductsTableDef.storeProducts;
    }


    @Override
    public boolean updateEnabled(String storeUid, String uid, Integer enabled, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2));
        return UpdateChain.of(StoreProducts.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public int dropBean(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public boolean haveName(String storeUid, String uid, String productName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.productName.eq(productName))
                .and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2));
        if (StringUtils.isNotBlank(uid))
            condition.and(table.uid.ne(uid));
        return exists(condition);
    }

    @Override
    public StoreProducts getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public int getMaxOrderNum(String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .from(table)
                .where(condition);

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }

    @Override
    public StoreProducts getGtShowOrder(String storeUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderNum.gt(orderNum))
                .and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public StoreProducts getLtShowOrder(String storeUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderNum.lt(orderNum))
                .and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public boolean updateOrderNum(String storeUid, String uid, Integer orderNum, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2))
                .and(table.uid.eq(uid));
        return UpdateChain.of(StoreProducts.class)
                .set(table.orderNum, orderNum)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public StoreProducts getByStoreUidOrUid(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.storeUid.eq(storeUid));

        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public Page<StoreProducts> storePageList(PageUtil pageUtil, String storeUid, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.productName.eq(keyword));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));
        condition.and(table.storeUid.eq(storeUid))
                .and(table.source.eq(2));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(getDto())
                .from(table)
                .where(condition)
                .orderBy(table.source.asc(), table.orderNum.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public List<StoreProducts> getListV2(String storeUid, String keyword, Integer productType, String cooperationTypeSql, String operationTypeSql) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.productName.like(keyword));
        condition.and(table.source.eq(1).and(cooperationTypeSql).and(operationTypeSql)
                        .or(table.source.eq(2).and(table.storeUid.eq(storeUid))))
                .and(table.enabled.eq(1));
        if (null != productType)
            condition.and(table.productType.eq(productType));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.source.asc(), table.orderNum.asc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<StoreProductDto> getDtoListV2(String storeUid, String cooperationTypeSql, String operationTypeSql) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.source.eq(1).and(cooperationTypeSql).and(operationTypeSql)
                        .or(table.source.eq(2).and(table.storeUid.eq(storeUid))))
                .and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.source.asc(), table.orderNum.asc());

        return getMapper().selectListByQueryAs(wrapper, StoreProductDto.class);
    }

    @Override
    public List<StoreProducts> listByUids(List<String> productUids) {
        return getMapper().selectListByCondition(table.uid.in(productUids));
    }

    private QueryColumn[] getDto() {
        return new QueryColumn[]{table.uid, table.storeUid, table.productName, table.orderNum,
                table.showStatus, table.productType, table.projectNum, table.enabled, table.source, table.remark,
                table.addTime, table.addStaffUid, table.addStaffName, table.editTime,
                table.lastStaffUid, table.lastStaffName, table.cooperationType
        };
    }

}
